package com.lcx.plugin.neteaseCloud.music.api;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lcx.plugin.neteaseCloud.util.NetWorkUtil;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author daxiong
 * @date 2021/9/7 下午4:34
 */
public class Demo {


    public static void main(String[] args) {

        String cookie = "__remember_me=true; Max-Age=1296000; Expires=Wed, 27 Oct 2021 08:34:00 GMT; Path=/;;MUSIC_A_T=1520220824210; Max-Age=2147483647; Expires=Sun, 30 Oct 2089 11:48:07 GMT; Path=/;;MUSIC_U=9af1dc05deab79e5e016f5cff36b44253d26b507fad93614a85a1a2c9211aed2993166e004087dd3d78b6050a17a35e705925a4e6992f61d07c385928f88e8de; Max-Age=1296000; Expires=Wed, 27 Oct 2021 08:34:00 GMT; Path=/;;MUSIC_R_T=1520220828812; Max-Age=2147483647; Expires=Sun, 30 Oct 2089 11:48:07 GMT; Path=/;;__csrf=433b95ccf635ff0f844e17df2b2a8d82; Max-Age=1296010; Expires=Wed, 27 Oct 2021 08:34:10 GMT; Path=/;;NMTID=00ORRW2iCVIoTDoBkarr7G_CjOijTUAAAF8c6MYHA; Max-Age=315360000; Expires=Fri, 10 Oct 2031 08:34:00 GMT; Path=/;";
        JSONObject jsonObject = JSON.parseObject(NetWorkUtil.sendByGetUrl("/playlist/detail?id=5225574874", cookie));
        System.out.println("jsonObject = " + jsonObject);
        System.out.println(jsonObject.getJSONObject("playlist").getJSONArray("tracks"));

    }

    public static String setPassword(String password, Boolean md5) {
        if (!md5) {
            return encodeByMD5(password);
        } else {
            return password;
        }
    }

    public static String encodeByMD5(String str) {
        byte[] secretBytes;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    str.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("md5 error");
        }
        StringBuilder md5code = new StringBuilder(new BigInteger(1, secretBytes).toString(16));
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code.insert(0, "0");
        }
        return md5code.toString();
    }
}
